﻿using Excel;
using PowerPoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word;

namespace drawing_print.util
{
    public class Wps
    {
        /// <summary>
        /// WORD文件打印
        /// </summary>
        /// <param name="sourcePath">文件路径</param>
        public static bool wordToPdf(string sourcePath, string outPath)
        {
            
            try
            {
                var type = Type.GetTypeFromProgID("KWps.Application");
                dynamic wps = Activator.CreateInstance(type);
                var doc = wps.Documents.Open(sourcePath, Visible: false);
                doc.ExportAsFixedFormat(outPath, WdExportFormat.wdExportFormatPDF);
                doc.Close();

                wps.Quit();
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return true;

        }

        public static bool excelToPdf(string sourcePath, string outPath)
        {
            
            try
            {
                var type = Type.GetTypeFromProgID("KET.Application");
                dynamic wps = Activator.CreateInstance(type);

                var targetType = XlFixedFormatType.xlTypePDF;
                var missing = Type.Missing;
                var doc = wps.Application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);
                doc.ExportAsFixedFormat(targetType, outPath, XlFixedFormatQuality.xlQualityStandard, true, false,
                    missing, missing, missing, missing);
                doc.Close();

                wps.Quit();
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return true;
        }



        public static bool powerPointToPdf(string sourcePath, string outPath)
        {
            
            try
            {
                var type = Type.GetTypeFromProgID("KWPP.Application");
                dynamic wps = Activator.CreateInstance(type);

                var doc = wps.Presentations.Open(sourcePath, MsoTriState.msoCTrue, MsoTriState.msoCTrue,
                    MsoTriState.msoCTrue);
                doc.SaveAs(outPath, PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue);
                doc.Close();

                wps.Quit();
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return true;
        }


        public static bool wpsToPdf(string sourcePath, string outPath, string extension)
        {
            if (".doc".Equals(extension) || ".docx".Equals(extension))
            {
                return Wps.wordToPdf(sourcePath, outPath);
            }
            else if (".xls".Equals(extension) || ".xlsx".Equals(extension))
            {
                return Wps.excelToPdf(sourcePath, outPath);
            }
            else if (".ppt".Equals(extension) || ".pptx".Equals(extension))
            {
               return Wps.powerPointToPdf(sourcePath, outPath);
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 判断文件类型是否支持
        /// </summary>
        /// <param name="extension"></param>
        /// <returns></returns>
        public static bool isSupport(string extension)
        {
            switch (extension)
            {
                // WORD类型
                case ".doc":
                    return true;
                case ".docx":
                    return true;
                // EXCLE类型
                case ".xls":
                    return true;
                case ".xlsx":
                    return true;
                // PPT类型
                case ".ppt":
                    return true;
                case ".pptx":
                    return true;
                default:
                    return false;
            }
        }
    }
}
